package org.dromara.xxmj.service.impl;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.xxmj.domain.dto.CourseQueryDto;
import org.dromara.xxmj.domain.vo.*;
import org.dromara.xxmj.mapper.CourseChapterMapper;
import org.dromara.xxmj.mapper.CourseMapper;
import org.dromara.xxmj.mapper.CourseOrderMapper;
import org.dromara.xxmj.mapper.UserStudyRecordMapper;
import org.dromara.xxmj.service.ICourseService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程服务实现类
 *
 * @author Vincent
 */
@RequiredArgsConstructor
@Service
public class CourseServiceImpl implements ICourseService {

    private final CourseMapper courseMapper;
    private final CourseOrderMapper orderMapper;
    private final UserStudyRecordMapper studyRecordMapper;
    private final CourseChapterMapper chapterMapper;

    /**
     * 获取课程列表
     *
     * @param dto 查询条件
     * @param pageQuery 分页参数
     * @return 课程列表
     */
    @Override
    public TableDataInfo<CourseListVO> listCourse(CourseQueryDto dto, PageQuery pageQuery) {
        String userId = LoginHelper.getUserIdStr();
        return TableDataInfo.build(courseMapper.selectCourseList(dto,userId, pageQuery));
    }

    /**
     * 获取课程详情
     *
     * @param id 课程ID
     * @return 课程详情
     */
    @Override
    public CourseDetailVo getCourseDetail(Long id) {
        CourseDetailVo vo = courseMapper.selectCourseDetail(id);
        if (vo != null) {
            // 检查当前用户是否已购买
            vo.setPurchased(checkUserPurchased(id));
        }
        return vo;
    }

    /**
     * 获取用户已购买的课程列表
     *
     * @param query 查询条件
     * @param pageQuery 分页参数
     * @return 课程列表
     */
    @Override
    public TableDataInfo<CourseMyOrderVO> listUserCourse(CourseQueryDto query, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        return TableDataInfo.build(courseMapper.selectUserCourseList(userId, query, pageQuery));
    }

    /**
     * 检查用户是否已购买课程
     *
     * @param courseId 课程ID
     * @return 是否已购买
     */
    @Override
    public Boolean checkUserPurchased(Long courseId) {
        Long userId = LoginHelper.getUserId();
        return courseMapper.checkUserPurchased(userId, courseId);
    }

    /**
     * 获取用户课程学习进度
     *
     * @param courseId 课程ID
     * @return 学习进度（百分比）
     */
    @Override
    public Integer getUserCourseProgress(Long courseId) {
        Long userId = LoginHelper.getUserId();
        Integer progress = studyRecordMapper.getUserCourseProgress(userId, courseId);
        return progress != null ? progress : 0;
    }

    /**
     * 获取课程章节列表
     *
     * @param courseId 课程ID
     * @return 章节列表
     */
    @Override
    public List<CourseChapterVo> getCourseChapters(Long courseId) {
        Long userId = LoginHelper.getUserId();
        // 获取章节列表，包含学习进度
        return chapterMapper.selectChaptersWithUserProgress(courseId, userId);
    }
}
